#!/bin/sh
echo auto-install java-mysql
yum install -y wget
#2. check if install local mysql rpm ,if not,install
    if [ ! -f mysql*.rpm ];then 
    wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    fi
echo -e "begin install rpm kagage\n-------------------------"  
    yum localinstall -y  mysql57-community-release-el7-11.noarch.rpm  
    echo -e "complete install mysql rpm package,these are mysql in yum repository list:"  
    yum repolist enabled|grep mysql  
    echo  

# 3.check if install mysql,if not,install

#    if [ `rpm -qa|grep mysql|wc -l` -gt 1 ];then  
    echo -e "yes,you have installed mysql:"  
    rpm -qa |grep mysql  
    echo  
 #   else  
    echo -e "you don't install mysql,now we start install mysql\n-----------------------"  
    yum install -y  mysql-community-server  
    echo -e "complete install mysql,these are the all installed:"  
    rpm -qa|grep mysql  
    echo  
 #   fi  

#4. start mysql
    echo -e "start mysql"  
    systemctl start mysqld  
    echo -e "mysql has started\n"  

#5. set mysql start when we start the CentOS

    echo -e "set mysql start when we start the CentOS"  
    systemctl enable mysqld  
    systemctl daemon-reload  
    echo -e "set succeed\n"  

# 6.login mysql use root and initial password

    USERNAME=root  
    INITIAL_PASSWORD=$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
 #regex and  last column(awk default split by space,if you want last-1: print$( NF-1 ) )
    echo -e "your initial mysql password for root is:\n$INITIAL_PASSWORD"  
    echo -e "login mysql: -----------------" 
    mysql -uroot -p$INITIAL_PASSWORD -e "set global validate_password_policy=0;" -b --connect-expired-password
    mysql -uroot -p$INITIAL_PASSWORD -e "set global validate_password_length=6;" -b --connect-expired-password
    mysql -uroot -p$INITIAL_PASSWORD -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';" -b --connect-expired-password
    echo -e "\n mysql root password:123456"

	echo -e "start install jdk1.8"
if type -p java; then
    echo 'Java has been installed.'
    exit 0
else
    echo 'start install java...'
    java_home=/usr/local/jdk8
    if [ ! -f jdk*.gz ];then
    wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
    fi
    mkdir -p ${java_home}
    tar -xvf jdk-8u141-linux-x64.tar.gz -C ${java_home} --strip-components 1
    echo "export PATH=$PATH:${java_home}/bin" >> /etc/profile
    echo 'setting path successful.'
    source /etc/profile
    echo 'install successful.'
fi

sleep 1
source /etc/profile
